package com.neuedu.funlifebbs.controller;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.neuedu.funlifebbs.bean.Categorys;
import com.neuedu.funlifebbs.bean.Labels;
import com.neuedu.funlifebbs.query.LabelsQuery;
import com.neuedu.funlifebbs.service.CategorysService;
import com.neuedu.funlifebbs.service.LabelsService;
import com.neuedu.funlifebbs.utils.JsonModel;

/**
 * 
 * @author myh
 * @version 2019年8月19日下午1:38:44
 *
 */
@Controller
@RequestMapping("admin/labels")
public class LabelsController {

	@Autowired
	LabelsService labelsService;

	@Autowired
	CategorysService categorysService;

	/**
	 * 转发到标签列表页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/listUI", method = RequestMethod.GET)
	public String listUi() {
		return "back/labels/list";
	}

	/**
	 * 分页查询并显示在标签列表页面
	 * 
	 * @param categorysQuery
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public JsonModel list(LabelsQuery labelsQuery) throws IOException {
		PageInfo<Labels> pageInfo = labelsService.query(labelsQuery);
		JsonModel jsonModel = new JsonModel();
		jsonModel.setMsg("查找成功");
		jsonModel.setSuccess(true);
		jsonModel.setTotal(pageInfo.getTotal());
		jsonModel.setRows(pageInfo.getList());
		return jsonModel;
	}

	/**
	 * 转发到添加标签页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String addUi(Model model) {
		List<Categorys> list = categorysService.findAll();
		model.addAttribute("categorys", list);
		return "back/labels/add";
	}

	/**
	 * 添加新标签
	 * 
	 * @param categorys
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(Labels labels) {

		if (labels.getId() != 0) {
			labelsService.update(labels);
		} else {
			labelsService.add(labels);
		}
		return "redirect:/admin/labels/listUI";
	}

	/**
	 * 根据标签ID编辑版块
	 * 
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String editUi(Model model, @PathVariable int id) {
		Labels labels = labelsService.findById(id);
		List<Categorys> list = categorysService.findAll();
		model.addAttribute("categorys", list);
		model.addAttribute("labels", labels);
		return "back/labels/edit";
	}

	/**
	 * 根据标签ID删除版块
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	@ResponseBody
	public JsonModel delete(@PathVariable int id) {
		labelsService.deleteById(id);
		JsonModel jsonModel = new JsonModel();
		jsonModel.setSuccess(true);
		jsonModel.setMsg("删除成功");
		return jsonModel;
	}

}
